import { ShellUtil } from "./ShellUtil";
import { Main } from "../Main";
import { PicCompressUtil } from "./PicCompressUtil";
import { Csd2LuaUtil } from "./Csd2LuaUtil";
import { ConfigUtil } from "./ConfigUtil";
import { DirUtil } from "./DirUtil";
import { PicMergeUtil } from "./PicMergeUtil";
import { SyncResInfoUtil } from "./SyncResInfoUtil";

/**
 * 资源同步，执行工具
 */
export class SyncResExecUtil {
    static exec(type: "normal" | "ios" | "android", needExportList: string[], needDeleteList: string[], cb: () => void) {
        this.execDelete(type, needDeleteList, () => {
            console.log("delete ok");
            this.execExport(type, needExportList, () => {
                console.log("export ok");
                cb();
            });
        });
    }
    /** 执行删除 */
    static execDelete(type: "normal" | "ios" | "android", needDeleteList: string[], cb: () => void) {
        let resTmpPath = type == "normal" ? "res/" : (type == "ios" ? "resPlatform/resIos/" : "resPlatform/resAndroid/");
        let overCount = 0;
        if (needDeleteList.length == 0) {
            cb();
            return;
        }
        for (let i = 0; i < needDeleteList.length; i++) {
            const file = needDeleteList[i];
            ShellUtil.exec(`
                rm -rf "${Main.codePath}Resources/${resTmpPath}${file}" || exit 1
                `, () => {
                overCount++;
                console.log(`syncres:delete:${Math.floor(overCount / (needDeleteList.length) * 1000) / 10}%`, file);
                if (overCount == (needDeleteList.length)) {
                    cb();
                }
            });
        }
    }
    /** 执行导出 */
    static execExport(type: "normal" | "ios" | "android", needExportList: string[], cb: () => void) {
        let overCount = 0;
        console.log("needExportList.length", needExportList.length);
        if (needExportList.length == 0) {
            cb();
            return;
        }
        for (let i = 0; i < needExportList.length; i++) {
            const file = needExportList[i];
            this.execExportOne(type, file, () => {
                overCount++;
                console.log(`syncres:export:${Math.floor(overCount / (needExportList.length) * 1000) / 10}%`, overCount, file);
                if (overCount == needExportList.length) {
                    cb();
                }
            });
        }
    }
    /** 执行导出一个文件 */
    static execExportOne(type: "normal" | "ios" | "android", file: string, cb: () => void) {
        console.log("execExportOne", file, type);
        let resTmpPath = type == "normal" ? "res/" : (type == "ios" ? "resPlatform/resIos/" : "resPlatform/resAndroid/");
        if (file.endsWith("/")) {
            if (type != "normal" && SyncResInfoUtil.isNoPkmPvr(file)) {
                type = "normal";// 处理安卓不导出pkm的
            }
            // 合图
            PicMergeUtil.merge(
                type,
                `${Main.codePath}Resources/cocosstudio/cocosstudio/${file}`,
                `${Main.codePath}Resources/${resTmpPath}${file.substr(0, file.length - 1)}`,
                file,
                () => {
                    if (type == "normal" && this.checkCompress(file)) {
                        PicCompressUtil.compressPng(`${Main.codePath}Resources/${resTmpPath}${file.substr(0, file.length - 1)}.png`, cb)
                    } else {
                        cb();
                    }
                }
            )
        } else if (file.endsWith(".png")) {
            // png图片
            this.execExportNormalFile(type, file, () => {
                if (this.checkCompress(file)) {
                    PicCompressUtil.compressPng(`${Main.codePath}Resources/${resTmpPath}${file}`, cb)
                } else {
                    cb();
                }
            });
        } else if (file.endsWith(".jpg")) {
            // jpg图片
            this.execExportNormalFile(type, file, () => {
                if (this.checkCompress(file)) {
                    PicCompressUtil.compressJpg(`${Main.codePath}Resources/${resTmpPath}${file}`, cb)
                } else {
                    cb();
                }
            });
        } else if (file.endsWith(".csd")) {
            // csd文件
            Csd2LuaUtil.export(
                `${Main.codePath}Resources/cocosstudio/cocosstudio/${file}`,
                `${Main.codePath}Resources/${resTmpPath}${file.replace(".csd", ".lua")}`,
                cb
            )
        } else {
            console.log("normalFile");
            this.execExportNormalFile(type, file, cb);
        }
    }
    /** 导出普通文件 */
    static execExportNormalFile(type: "normal" | "ios" | "android", file: string, cb: () => void) {
        let resTmpPath = type == "normal" ? "res/" : (type == "ios" ? "resPlatform/resIos/" : "resPlatform/resAndroid/");
        DirUtil.confirmDirExist(`${Main.codePath}Resources/${resTmpPath}${file}`, () => {
            ShellUtil.exec(`
                cp -rf "${Main.codePath}Resources/cocosstudio/cocosstudio/${file}" "${Main.codePath}Resources/${resTmpPath}${file}" || exit 1
            `, () => {
                cb();
            });
        });
    }
    /** 检测图片是否需要压缩 */
    static checkCompress(pic: string): boolean {
        // return false;
        for (let i = 0; i < ConfigUtil.NoCompressPicCfg.dirs.length; i++) {
            const dir = ConfigUtil.NoCompressPicCfg.dirs[i];
            if (pic.startsWith(dir)) {
                // console.log("pic", pic);
                return false
            }
        }
        // console.log(" checkCompress true pic", pic);
        return true;
    }
}